package com.morningstar.qs.api.basic.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Comparator;


/**
 * This abstract class wraps type-file resource as a FileUtil object. 
 * Provides some methods to read and write this resource.   
 * @author dchen2
 *
 */
@Deprecated
public abstract class FileUtil {
	
	/**
	 * Resource name
	 */
	protected String fileName;

	/**
	 * Read a file from File System, which will be covert to specific format
	 * @return specified data format
	 * @throws FileNotFoundException 
	 * @throws IOException 
	 * @throws FileFormatException 
	 */
	public abstract Object readFile() throws FileNotFoundException, IOException;
	
	/**
	 * Write specific data to file
	 * @param object data
	 */
	public abstract void writeFile(Object object);
	

	@SuppressWarnings("unchecked")
	public static File[] dirListByAscendingDate(File folder) {
		if (!folder.isDirectory()) {
			return null;
		}
		File files[] = folder.listFiles();
		Arrays.sort(files, new Comparator() {
			public int compare(final Object o1, final Object o2) {
				return new Long(((File) o1).lastModified())
						.compareTo(new Long(((File) o2).lastModified()));
			}
		});
		return files;
	}

	@SuppressWarnings("unchecked")
	public static File[] dirListByDescendingDate(File folder) {
		if (!folder.isDirectory()) {
			return null;
		}
		File files[] = folder.listFiles();
		Arrays.sort(files, new Comparator() {
			public int compare(final Object o1, final Object o2) {
				return new Long(((File) o2).lastModified())
						.compareTo(new Long(((File) o1).lastModified()));
			}
		});
		return files;
	}


}
